<template>
  <div class="login-page">
    <div class="login-page-header">
      <Header></Header>
    </div>

    <div class="login-page-main">
      <div class="login-page-main-text1" @click="$router.push('/')">诚信商家火爆招商</div>
      <div class="login-page-main-text2" @click="$router.push('/')">诚信商家火爆招商</div>
      <div class="login-page-main-inputbox newBox">
        <div class="login-page-main-inputbox-logo">
          <img src="../../assets/image/header/logo.png" alt="" />
        </div>
        <div class="login-page-main-inputbox-tab">
          <el-tabs v-model="activeName" style="font-size: 0.16rem">
            <el-tab-pane label="账号密码登录" name="first">
              <div class="firstForm">
                <el-form
                  ref="loginForm"
                  :model="form"
                  :rules="rules"
                  style="width: 100%; margin-top: 0.2rem"
                >
                  <el-form-item prop="username">
                    <el-input
                      placeholder="用户名"
                      v-model="form.username"
                      prefix-icon="el-icon-user"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item prop="password">
                    <el-input
                      :type="flagType"
                      placeholder="密码"
                      v-model="form.password"
                    >
                      <i
                        slot="suffix"
                        :class="[
                          this.flag
                            ? 'iconfont icon-yanjing-zhengyan'
                            : 'iconfont icon-biyan',
                        ]"
                        style="margin-top: 8px; font-size: 18px"
                        @click="getFlag()"
                      ></i>
                    </el-input>
                  </el-form-item>
                </el-form>
                <el-row
                  type="flex"
                  justify="space-between"
                  style="margin-top: 20px"
                >
                  <el-col :span="12"
                    ><div class="left" @click="$router.push('/wjmm')">
                      忘记密码
                      <i
                        class="el-icon-question"
                        style="color: rgb(187, 184, 184)"
                      ></i></div
                  ></el-col>
                  <el-col :span="12"
                    ><div class="right" @click="$router.push('/enroll')">
                      没有账号，去注册>>
                    </div></el-col
                  >
                </el-row>
                <div class="errorBox" v-show="showError">
                  <div class="errorBox-left">
                    <i
                      class="el-icon-error"
                      style="color: #fa5555; margin-right: 0.1rem"
                    ></i
                    >{{ text }}
                  </div>
                  <div class="errorBox-right">
                    <i
                      class="el-icon-close"
                      style="cursor: pointer; color: #c0c4cc"
                      @click="showError = false"
                    ></i>
                  </div>
                </div>
                <div class="loginbutton" @click="login">登录</div>
              </div>
            </el-tab-pane>

            <el-tab-pane label="手机号登录" name="second">
              <div class="secondForm">
                <el-form
                  ref="phoenform"
                  :model="phoneform"
                  :rules="phonerules"
                  style="width: 100%"
                >
                  <el-form-item prop="telephone">
                    <el-input
                      placeholder="手机号"
                      v-model="phoneform.telephone"
                      prefix-icon="el-icon-mobile"
                    >
                    </el-input>
                  </el-form-item>
                  <el-form-item style="width: 100% !important" prop="ImgCode">
                    <div
                      style="
                        width: 100% !important;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                      "
                    >
                      <el-input
                        style="width: 2.5rem !important"
                        placeholder="图形验证码"
                        v-model="phoneform.ImgCode"
                      >
                      </el-input>
                      <!-- <validate-code ref="ref_validateCode" /> -->
                      <img
                        :src="codeImg"
                        @click="getCode"
                        class="codeImg"
                        alt=""
                      />
                    </div>
                  </el-form-item>
                  <el-form-item style="width: 100% !important" prop="authCode">
                    <div class="getCode">
                      <el-input
                        placeholder="手机验证码"
                        v-model="phoneform.authCode"
                      >
                      </el-input>
                      <div class="getCodeButton" @click="getSmsB" v-if="show">
                        获取验证码
                      </div>
                      <div class="getCodeButton" v-else>
                        {{ time }}秒后重新获取
                      </div>
                    </div>
                  </el-form-item>
                  <div class="loginbutton" @click="phoneLogin">登录</div>
                  <div class="message" @click="$router.push('/enroll')">
                    没有账号，去注册>>
                  </div>
                </el-form>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>

    <div class="login-page-bottom">
      <Bottom></Bottom>
    </div>
  </div>
</template>

<script>
import { getCode, getSms } from "@/api/enroll";
import md5 from "js-md5";
import Header from "../../components/header/header.vue";
import validateCode from "@/components/ValidateCode";
import Bottom from "../../components/bottom";
import { v4 as uuidv4 } from "uuid";
export default {
  components: {
    Header,
    validateCode,
    Bottom,
  },
  data() {
    const sjhreg =
      /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      const mmreg = /(?=.*?[A-Z].*?)(?=.*?[a-z].*?)(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{12,12}/;
    const validatesjh = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入手机号"));
      } else if (!sjhreg.test(value)) {
        callback(new Error("请检查手机号码是否正确"));
      } else {
        callback();
      }
    };
    const validatemm = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (!mmreg.test(value)) {
          callback(new Error("请输入12位，需包含大小写字母、数字以及字符"));
        }
        callback();
      }
    };
    return {
      flag: false,
      flagType: "password",
      show: true,
      time: 60,
      codeImg: "",
      text: "",
      showError: false,
      activeName: "first",
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, validator: validatemm, trigger: "blur" }],
      },
      phoneform: {},
      phonerules: {
        telephone: [
          { required: true, validator: validatesjh, trigger: "blur" },
        ],
        ImgCode: [
          { required: true, message: "请输入图形验证码", trigger: "blur" },
        ],
        authCode: [
          { required: true, message: "请输入手机验证码", trigger: "blur" },
        ],
      },
      uuid:''
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    // 小眼睛切换状态
    getFlag() {
      this.flag = !this.flag;
      this.flagType = this.flag ? "text" : "password"; // text为显示密码；password为隐藏密码
    },
    // 手机验证码登陆
    phoneLogin() {
      this.$refs.phoenform.validate((valid) => {
        if (valid) {
          const params = {
            telephone: this.phoneform.telephone,
            authCode: this.phoneform.authCode,
          };
          this.$store
            .dispatch("phoneLogin", params)
            .then((res) => {
              if (res.code == 200) {
                this.$router.push({ path: "/home" });
              } else {
              }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    login() {
      this.showError = false;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const newForm = JSON.parse(JSON.stringify(this.form));
          newForm.password = md5(newForm.password);
          this.$store
            .dispatch("Login", newForm)
            .then((res) => {
              if (res.code == 200) {
                this.$router.push({ path: "/home" });
                setCookie("user", this.form.username, 15);
                setCookie("password", newForm.password, 15);
              } else {
                this.text = res.message;
                this.showError = true;
              }
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    // 获取图形验证码
    getCode() {
      this.uuid = (uuidv4() + "").replace(/-/g, "").substring(0, 5);
      getCode(this.uuid).then((data) => {
        this.codeImg = `data:image/jpg;base64,${data.data}`;
      });
    },
    // 获取短信验证码
    getSmsB() {
      this.$refs.phoenform.validateField(["ImgCode"], (errorMessage) => {
        const valid = errorMessage == "";
        if (valid) {
          this.$refs.phoenform.validateField(["telephone"], (errorMessage) => {
            const valid = errorMessage == "";
            if (valid) {
              const params = {
                mobile: this.phoneform.telephone,
                imageCode: this.phoneform.ImgCode,
                type: "LOGIN",
              };

              getSms(params,this.uuid).then((data) => {
                if (data.code == 200) {
                  this.$message({
                    message: "短信验证码发送成功",
                    type: "success",
                  });
                  this.show = false;
                  const timer = setInterval(() => {
                    if (this.time > 0) {
                      this.time--;
                    } else {
                      this.show = true;
                      this.time = 60;
                      clearInterval(timer);
                    }
                  }, 1000);
                } else {
                  this.getCode();
                }
              });
            } else {
              return false;
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
/deep/ .el-tabs__header {
  margin: 0 0 0.15rem;
}
.login-page {
  width: 100%;
  height: 100vh;
  &-header {
    width: 100%;
    height: 0.96rem;
  }
  &-main {
    width: 100%;
    height: 6.63rem;
    background-image: url("../../assets/image/login/bg.png");
    background-size: 100% 100%;
    background-repeat: no-repeat;
    position: relative;
    &-text1 {
      font-size: 0.72rem;
      font-weight: bold;
      position: absolute;
      top: 1.29rem;
      left: 3.34rem;
      color: #ffffff;
      cursor:pointer;
    }
    &-text2 {
      font-size: 0.36rem;
      font-weight: normal;
      position: absolute;
      top: 2.31rem;
      left: 4.78rem;
      color: #ffffff;
      cursor:pointer;
    }
    &-inputbox {
      width: 5.31rem;
      height: 4.82rem;
      background: #ffffff;
      border-radius: 0.1rem;
      position: absolute;
      top: 0.91rem;
      left: 10.89rem;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      &-logo {
        width: 100%;
        height: 0.8rem;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          height: 0.52rem;
          width: auto;
        }
      }
      &-tab {
        width: 3.78rem;
        height: 3.5rem;
        .loginbutton {
          margin-top: 0.12rem;
          width: 100%;
          height: 0.4rem;
          border-radius: 0.02rem;
          opacity: 0.5;
          background: #e22a1f;
          text-align: center;
          line-height: 0.4rem;
          color: #ffffff;
          font-size: 0.16rem;
          letter-spacing: 0.04rem;
          cursor: pointer;
        }
        .firstForm {
          .left {
            text-align: left;
            color: #666666;
            cursor: pointer;
          }
          .right {
            text-align: right;
            color: #e22a1f;
            cursor: pointer;
          }
          .errorBox {
            width: 100%;
            height: 0.4rem;
            margin-top: 0.1rem;
            background: #fef0f0;
            border: 1px dashed #fde2e2;
            display: flex;
            justify-content: space-between;
            align-items: center;
            &-left {
              width: auto;
              height: 0.4rem;
              line-height: 0.4rem;
              text-align: left;
              padding: 0 0.1rem;
              font-family: PingFang SC;
              font-size: 0.12rem;
              color: #e22a1f;
            }
            &-right {
              width: 0.2rem;
              height: 0.4rem;
              line-height: 0.4rem;
              text-align: right;
              padding: 0 0.1rem;
            }
          }
          ::v-deep .el-form-item__content,
          .el-input {
            width: 100% !important;
          }
        }
        .secondForm {
          .getCode {
            width: 100% !important;
            height: 0.4rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
            ::v-deep .el-input__inner {
              height: 0.4rem !important;
            }
          }
          ::v-deep .el-form-item__content {
            // width: 100% !important;
            margin-bottom: 10px;
          }
          .codeImg {
            width: 100px;
            height: 0.4rem;
            cursor: pointer;
          }
          .getCodeButton {
            position: absolute;
            right: 0;
            top: 0;
            width: calc(100% - 2.5rem);
            height: 0.38rem;
            line-height: 0.38rem;
            text-align: center;
            cursor: pointer;
            color: #fa5555;
          }
          .message {
            margin-top: 0.1rem;
            width: 100%;
            text-align: center;
            font-family: PingFang SC;
            font-size: 0.14rem;
            color: #e22a1f;
            cursor: pointer;
          }
        }

        ::v-deep .el-form-item__content {
          width: 100% !important;
        }
        ::v-deep .el-tabs__item {
          font-size: 0.16rem;
          font-weight: bold;
        }
        ::v-deep .el-tabs__item:hover {
          color: red !important;
        }
        ::v-deep .el-tabs__item.is-active {
          color: red !important;
        }
        ::v-deep .el-tabs__active-bar {
          height: 0.04rem;
          background-color: red !important;
        }
        ::v-deep .el-input__prefix {
          color: #3d3c3c !important;
        }
        ::v-deep .el-input__inner {
          height: 0.4rem !important;
        }
        ::v-deep .el-tabs__nav-wrap::after {
          background: none;
        }
      }
    }
  }
  &-bottom {
    width: 100%;
    height: 3.2rem;
  }
}
</style>
<style scoped lang="scss">
@media screen and (max-width: 1300px) {
  .newBox{
    height: 5rem !important;
  }
}
</style>